package com.xiaocg.juc.sync;

import java.util.ArrayList;

public class RoomTest {
    public static void main(String[] args) throws Exception{
        Room room = new Room();
        Thread thread1 = new Thread(()->{
            for (int i=0;i<5000;i++){
                room.increment();
            }
        });

        Thread thread2 = new Thread(()->{
            for (int i=0;i<6000;i++){
                room.decrement();
            }
        });

        thread1.start();
        thread2.start();
        thread1.join();
        thread2.join();
        System.out.println("count:"+room.getCounter());
    }
}

class Room {
    private int counter=0;
    public void increment(){
        synchronized (this){
            counter++;
        }
    }

    public void decrement(){
        synchronized (this){
            counter--;
        }
    }

    public int getCounter() {
        synchronized (this){
            return counter;
        }
    }
}
